package com.wiserun.RestFul.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Base64;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
import com.wiserun.develop.file.model.S_File;
import com.wiserun.develop.file.model.S_FileDTO;



@Component
public class Office2Pdf {
	private static final Logger logger = LoggerFactory.getLogger(Office2Pdf.class);
	
	@Value("${fileConvert.command}")
	private String command= "C:/Program Files (x86)/OpenOffice.org 3/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
	@Value("${fileConvert.host}")
	private String host="127.0.0.1";
	@Value("${fileConvert.port}")
	private int port=8100;
	
	public  boolean office2pdf(String srcPath, String desPath) throws IOException {
        // 源文件目录
        File inputFile = new File(srcPath);
        logger.info(srcPath);
        logger.info(desPath);
        if (!inputFile.exists()) {
        	logger.error("源文件不存在！");
            return false;
        }
        // 输出文件目录
        File outputFile = new File(desPath);
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().exists();
        }
        // 调用openoffice服务线程
        //String command = "C:/Program Files (x86)/OpenOffice.org 3/program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
        Process p = Runtime.getRuntime().exec(command);

        // 连接openoffice服务
        OpenOfficeConnection connection = new SocketOpenOfficeConnection(host, port);
        connection.connect();

        // 支持转换txt doc ppt xls文件到pdf
        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
        converter.convert(inputFile, outputFile);

        // 关闭连接
        connection.disconnect();

        // 关闭进程
        p.destroy();
        logger.info("转换完成！");
        return true;
    }
	
	public String fileToBase64(String filePath) throws Exception {
		    File file = new File(filePath);
		    if(file.exists()) {
		    	FileInputStream fin = new FileInputStream(filePath);
				BufferedInputStream bin = new BufferedInputStream(fin);  
				ByteArrayOutputStream baos= new ByteArrayOutputStream();
				BufferedOutputStream bout = new BufferedOutputStream(baos);
		    	byte[] buffer = new byte[1024];
		    	int len = bin.read(buffer);
		    	while(len!=-1) {
		    		bout.write(buffer,0,len);
		    		len = bin.read(buffer);
		    	}
		    	bout.flush();
		    	byte[] bytes=baos.toByteArray();
		    	String outString=Base64.getEncoder().encodeToString(bytes);
		    	//outString=outString.replaceAll("\r\n","");//替换换行和空格
				//outString=outString.replaceAll("\\+","%2B");//对base64的+转义
		    	return outString;
		    }else {
		    	 logger.error("待转换的文件不存在");
		    }
			return "";
	}
}
